/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [FontToolBar.java]
 * Creation Date        [11-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_02_gui.notepad_V1.ui.font;

import java.awt.FlowLayout;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JToolBar;

/**
 * 字体浮动工具栏
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class FontToolBar extends JToolBar {

	private static final long serialVersionUID = -7756550543940086384L;

	private ActionListener listener = null; // 字体菜单监听器

	private Icon boldIcon = null;
	private Icon italicIcon = null;
	private Icon underlineIcon = null;

	private JButton boldButton = null;
	private JButton italicButton = null;
	private JButton underlineButton = null;

	private String fontName; // 字体名称, 字体大小
	private int fontSize;

	private JComboBox<String> fontTypeBox = null;// 字体类型下拉列表框
	private JComboBox<String> fontSizeBox = null;// 字体大小下拉列表框

	/**
	 * 初始化字体类型
	 */
	private void initFontType() {
		// 获取本地所有的字体
		String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getAvailableFontFamilyNames();
		fontTypeBox = new JComboBox<String>(fonts);
		fontTypeBox.setActionCommand("Type");
		fontTypeBox.addActionListener(listener);
		// 添加监听器获取用户选择的字体
		fontTypeBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				fontName = (String) e.getItem();
			}
		});
		this.add(fontTypeBox);
	}

	/**
	 * 初始化字体大小
	 */
	private void initFontSize() {
		this.addSeparator();
		JLabel sizeLabel = new JLabel("大小");
		this.add(sizeLabel);

		String[] fontSizes = { "8", "9", "10", "11", "12", "14", "16", "18",
				"20", "22", "24", "26", "28", "36", "48", "72" };
		fontSizeBox = new JComboBox<String>(fontSizes);
		fontSizeBox.setActionCommand("Size");
		fontSizeBox.addActionListener(listener);
		// 添加监听器获取用户选择字体的大小
		fontSizeBox.addItemListener(new ItemListener() {
			@Override
			public void itemStateChanged(ItemEvent e) {
				fontSize = Integer.parseInt((String) e.getItem());
			}
		});
		this.add(fontSizeBox);
	}

	/**
	 * 初始化文件浮动工具栏
	 */
	private void init() {
		boldIcon = new ImageIcon(
				"src_03_best_practices/pkg_02_gui//notepad_V1/icons/bold.gif");
		italicIcon = new ImageIcon(
				"src_03_best_practices/pkg_02_gui//notepad_V1/icons/italic.gif");
		underlineIcon = new ImageIcon(
				"src_03_best_practices/pkg_02_gui//notepad_V1/icons/underline.gif");

		boldButton = new JButton(boldIcon);// 粗体
		italicButton = new JButton(italicIcon);// 斜体
		underlineButton = new JButton(underlineIcon);// 下划线

		boldButton.addActionListener(listener);
		italicButton.addActionListener(listener);
		underlineButton.addActionListener(listener);

		boldButton.setToolTipText("粗体");// 粗体
		italicButton.setToolTipText("斜体");// 斜体
		underlineButton.setToolTipText("下划线");// 下划线

		this.add(boldButton);
		this.add(italicButton);
		this.add(underlineButton);
		underlineButton.setEnabled(false); // 禁用下划线功能
		this.addSeparator();

		initFontType();
		initFontSize();

		/**
		 * 初始化浮动工具栏
		 */
		setFloatable(true); // 设置工具栏为可浮动
		FlowLayout flowLayout = new FlowLayout(FlowLayout.LEADING, 0, 0);
		setLayout(flowLayout);
		flowLayout.setAlignOnBaseline(true);
	}

	public FontToolBar(ActionListener listener) {
		this.listener = listener;
		init();
	}

	public JButton getBoldButton() {
		return boldButton;
	}

	public JButton getItalicButton() {
		return italicButton;
	}

	public JButton getUnderlineButton() {
		return underlineButton;
	}

	public String getFontName() {
		return fontName;
	}

	public int getFontSize() {
		return fontSize;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public JComboBox<String> getFontTypeBox() {
		return fontTypeBox;
	}

	public JComboBox<String> getFontSizeBox() {
		return fontSizeBox;
	}
}
